#! /usr/bin/perl -w
#
# program to list minc files with attributes

# output format:

my($file);
my($output);

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      @<<<<<<<<<<<<<<<<<<<<<<<<
$file,$output
.

my($fnames_found) = 0;
my($attribute_found) = 0;

foreach $argnum (0 .. $#ARGV)
{
    $_ = $ARGV[$argnum];
    
    if (/^-prot(ocol)?$/)
    {
	$attribute = 'dicom_0x0008:el_0x103e';
	$attribute_found = 1;
    }
    elsif (/^-seq(uence)?$/) {
	$attribute = 'acquisition:scanning_sequence';
	$attribute_found = 1;
    }
    elsif (/^-dur(ation)?$/) {
	$attribute = 'acquisition:scan_duration';
	$attribute_found = 1;
    }
    elsif (/^-start(_time)?$/) {
	$attribute = 'acquisition:start_time';
	$attribute_found = 1;
    }
    elsif (/^-co(il)?$/) {
	$attribute = 'acquisition:receive_coil';
	$attribute_found = 1;
    }
    elsif (/^-res(olution)?$/) {
	$attribute = 'xspace:step';
	$attribute_found = 1;
    }
    elsif (/^-h(elp)?$/) {
	die "Usage: lsminc <-attribute> <files>\nwhere attribute = one <protocol|sequence|duration|start_time|coil|resolution>, (default = protocol)\n";
    }
    elsif (/^-/)
    {
	die ("lsminc:  Unknown option: $_\n");
    }
    else
    {
	if (!$fnames_found)
	{
	    @files = @ARGV[$argnum..$#ARGV];
	    $fnames_found = 1;
	}
    }
}

if (!$attribute_found) {
#    $attribute = 'acquisition:protocol_name';
#    for 'Moco Series' string - mferre 07.03.13
    $attribute = 'dicom_0x0008:el_0x103e';
}

my($cnt) = 0;
# if no files found, just do current dir
if (!$fnames_found) {
    @files = <*.mnc*>;
}
else { # if any filenames lack .mnc extension, assume directory:

    foreach $file (@files) {
	unless ($file =~ /\.mnc/ ) {
	    $file = $file . '/*.mnc*';
	    my(@newfiles) = glob($file);
	    splice(@files,$cnt,1,@newfiles);
	}
	$cnt++;
    }
}

my($numfiles) = scalar(@files);

unless ($numfiles > 0) {
    die "lsminc:  No minc files found\n";
}

# sort files for correct numeric order

my($hosttype) = `uname`;
chomp($hosttype);

if ($hosttype =~ /linux/i) {
    @files = `ls -v @files`;
} else { # this fails if there is a sl### extension:
    @files = sort {
	$atmp=$a;
	$btmp=$b;
	$atmp =~ s/\D//g;
	$btmp =~ s/\D//g;
	$atmp <=> $btmp} @files;
}

foreach $file (@files) {

    chomp($file);
    # Print the directory of the current minc file
    # A Berman - 08/06/2011
    @dir_list = split(m!/!, $file);
    $dir_len = @dir_list;
    $dir = '';
    for ($i=0; $i < $dir_len-1; $i++) {
        $dir = $dir . $dir_list[$i] . '/';
    }
    print $dir;

    $output = `mincinfo -attvalue $attribute $file 2>&1`;
    chomp($output);
    if ($output =~ /not\ found/i) {
	$output = '(not found)';
	write (STDOUT);
	next;
    }

    if ($attribute eq 'acquisition:protocol_name' || $attribute eq 'dicom_0x0008:el_0x103e') {

	$output =~ s/.+\///; # kill all to last slash
	$output =~ s/\.prg//; # everything after .

	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);

    } 
    elsif ($attribute eq 'acquisition:scanning_sequence') {

	$output =~ s/.+\///; # kill all to last slash
	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);
    } 
    elsif ($attribute eq 'acquisition:scan_duration') {

	$output = $output/60;
	$output = sprintf("%5.2f",$output);

	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);
    } 
    elsif ($attribute eq 'acquisition:start_time') {

	$output =~ s/^.+\ //;
	$output =~ s/\..+$//; # everything after .
	
	$output = substr($output,0,2) . ':' . 
	    substr($output,2,2);
	$output =~ s/\s//g;

	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);

    } 
    if ($attribute eq 'acquisition:receive_coil') {

	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);
    } 
    if ($attribute eq 'xspace:step') {

	my($xstep) = `mincinfo -attvalue xspace:step $file 2>&1`;
	my($ystep) = `mincinfo -attvalue yspace:step $file 2>&1`;
	my($zstep) = `mincinfo -attvalue zspace:step $file 2>&1`;

	chomp($xstep);
	chomp($ystep);
	chomp($zstep);

	$xstep = abs($xstep);
	$ystep = abs($ystep);
	$zstep = abs($zstep);

        $output = sprintf("%3.1f x %3.1f x %3.1f",$xstep,$ystep,$zstep);

	$file =~ s/.+\///; # kill all to last slash
	write (STDOUT);

    } 
}




